﻿//using ET.CommonLibrary.Wcf;
using ET.Score.Service.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;

namespace DispatchingTelephonePOC
{

    public class AccountServiceProvider
    {
        public IAccountManageService helper = null;

        private static AccountServiceProvider instance = new AccountServiceProvider();

        public static AccountServiceProvider Get_Instance()
        {
            if (instance == null)
                instance = new AccountServiceProvider();
            return instance;
        }

        /// <summary>
        /// 获取主板信息
        /// </summary>
        /// <returns></returns>
        private static string GetUniqueIdentifier()
        {
            string identifier = "";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");

            foreach (ManagementObject board in searcher.Get())
            {
                identifier = board["SerialNumber"].ToString();
                break; // 只获取第一个主板的序列号
            }
            return identifier;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>[0]用户ID,[1]用户RealName,[2]考核ID(ExamID),[3]角色ID</returns>
        public string UserLogin(string username, string password)
        {
            try
            {
                //test 获取主板序列号
                string serialNumber = GetUniqueIdentifier();
                string retString = helper.SetUserLogin(username, password, serialNumber);
                return retString;
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error(ex.Message);
                return null;
            }
        }


        /// <summary>
        /// 获取当前步骤
        /// </summary>
        /// <param name="ExamID">考试ID</param>
        /// <returns>考试步骤</returns>
        public string GetCurrentStep(int ExamID)
        {
            try
            {
                return helper.GetCurrentStep(ExamID);
                //Exam_TestQuestion_Task_StepModel currentStep = JsonConvert.DeserializeObject<Exam_TestQuestion_Task_StepModel>(str);
                //return currentStep;
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error("获取当前考试步骤错误", ex);
                return "";
            }
        }

        /// <summary>
        /// 结束考试
        /// </summary>
        public void TerminateExam(int examID)
        {
            try
            {
                helper.TerminateExam(examID);
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error(ex.Message);
            }
        }


        /// <summary>
        /// 写入操作记录
        /// </summary>
        /// <param name="operationLog">操作日志类</param>
        public void WriteOperatedLog(string operationLog)
        {
            try
            {
                helper.WriteOperatedLog(operationLog);
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error("写入操作记录错误", ex);
            }
        }

        /// <summary>
        /// 开始考核
        /// </summary>
        /// <param name="examID"></param>
        public void StartExam(int examID)
        {
            try
            {
                helper.StartExam(examID);
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error(ex.Message);
            }
        }

        /// <summary>
        /// 获取场景文件
        /// </summary>
        /// <param name="examID"></param>
        /// <returns>场景文件byte[]</returns>
        public byte[] GetSceneFile(int examID)
        {
            try
            {
                byte[] retFile = helper.GetSceneFile(examID);
                if (retFile != null)
                {
                    return retFile;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error("获取场景文件错误", ex);
                return null;
            }
        }

        /// <summary>
        /// 获取是否可以考试
        /// </summary>
        /// <param name="examID"></param>
        /// <returns></returns>
        public bool GetPermission(int examID)
        {
            try
            {
                return helper.GetPermission(examID);
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error("获取是否可以考试错误", ex);
                return false;
            }
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        /// <param name="examID"></param>
        /// <param name="Permission"></param>
        public void SetPermission(int examID, bool Permission)
        {
            try
            {
                helper.SetPermission(examID, Permission);
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error(ex.Message);
            }
        }

        /// <summary>
        /// 考核后台允许登陆
        /// </summary>
        public bool AllowToLogin()
        {
            try
            {
                return helper.AllowToLogin();
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 设置列车信息
        /// 车组号_车次号_所属车站
        /// 转为byte[]数组
        /// </summary>
        /// <param name="trainInfo"></param>
        /// <param name="examID"></param>
        public void SetTrainInfomation(byte[] trainInfo, int examID)
        {
            try
            {
                helper.Set_TrainInformation(trainInfo, examID);
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error("设置列车信息错误", ex);
            }
        }

        /// <summary>
        /// 查询考试步骤
        /// </summary>
        /// <param name="examID">考试ID</param>
        /// <param name="stepID">步骤ID</param>
        /// <returns></returns>
        public string GetExamStepInfo(int examID, int stepID)
        {
            try
            {
                return  helper.GetExamStepInfo(examID, stepID);
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error("查询考试步骤错误", ex);
                return "";
            }
        }


        /// <summary>
        /// 设置虚拟时间
        /// </summary>
        /// <param name="examID">考试ID</param>
        /// <param name="virtualTime">虚拟时间</param>
        public void SetVirtualTime(int examID, DateTime virtualTime)
        {
            try
            {
                helper.SetVirtualTime(examID, virtualTime.ToString());
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error("设置虚拟时间错误", ex);
            }
        }

        /// <summary>
        /// 获取虚拟时间
        /// </summary>
        /// <param name="examID"></param>
        /// <returns></returns>
        public string GetVirtualTime(int examID)
        {
            try
            {
                return helper.GetVirtualTime(examID);
                //bool s = DateTime.TryParse(virtualTimeString, out var virtualTime);
                //if (s)
                //{
                //    return virtualTimeString;
                //}
                //else
                //{
                //    return DateTime.MinValue;
                //}
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error("获取虚拟时间错误", ex);
                //return DateTime.MinValue;
                return "";
            }
        }

        /// <summary>
        /// 完成仿真步骤后通知考核教员
        /// </summary>
        /// <param name="examID"></param>
        /// <param name="stepID"></param>
        public void UpdateStepStatus(int examID, int stepID)
        {
            try
            {
                helper.UpdateStepStatus(examID, stepID);
            }
            catch (Exception ex)
            {
                //LoggerHelper.LoggerHelper.Instance.Error("更新步骤状态错误", ex);

            }
        }
    }
}
